package com.tech.zhidengxian.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tech.zhidengxian.model.dataobject.ChatDO;
import com.tech.zhidengxian.model.dataobject.GoodsDO;
import com.tech.zhidengxian.model.dataobject.MessageDO;
import com.tech.zhidengxian.model.dto.request.GoodsRequest;

import java.util.List;

public interface ChatMapper extends BaseMapper<ChatDO> {
    default int creatChat(ChatDO chatDO){
        return insert(chatDO);
    }

    default int refreshLastMsg4Chat(ChatDO chatDO){
//        LambdaUpdateWrapper<ChatDO> wrapper = new LambdaUpdateWrapper<>();
//        wrapper.set(ChatDO::getLastMessage,chatDO.getLastMessage())
//                .eq(ChatDO::getId,chatDO.getId());
        return updateById(chatDO);
    }

    default List<ChatDO> queryChatListByUid(Long uid){
        LambdaQueryWrapper<ChatDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChatDO::getCreatorId,uid)
                .or(wp -> wp.eq(ChatDO::getReceiverId, uid).ne(ChatDO::getLastMessage, ""))
                .orderByDesc(ChatDO::getUpdateTime);
        return selectList(wrapper);
    }

    default ChatDO loadChat(ChatDO chatDO) {
        LambdaQueryWrapper<ChatDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChatDO::getCreatorId,chatDO.getCreatorId())
                .eq(ChatDO::getReceiverId,chatDO.getReceiverId())
                .eq(ChatDO::getGid,chatDO.getGid());
        return selectOne(wrapper);
    }
}
